package com.example.firstapp.activity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Spinner;

import com.example.firstapp.R;
import com.example.firstapp.adapter.foodTypeAdapter;
import com.example.firstapp.entity.Domain.FoodType;
import com.example.firstapp.entity.UserPreference;
import com.example.firstapp.entity.foodType;

public class ShowProfileActivity extends Activity {
	public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
	ArrayList foodType = new ArrayList<foodType>();
	// ArrayAdapter listAdapter;
	ListView listView;
	LinearLayout foodTypeLayout;
	LinearLayout remainingLayout;
	foodTypeAdapter foodAdapter;
	Spinner spinner;
	RadioButton cheapButton;
	RadioButton mediumButton;
	RadioButton expensiveButton;
	RatingBar ratingBar1;
	RatingBar ratingBar2;
	RatingBar ratingBar3;
	CheckBox buyGoCheck;
	SharedPreferences sharedPref;
	List<FoodType> listOfFoodType = new ArrayList<FoodType>();
	UserPreference userPref = new UserPreference();
	Set<String> foodTypesPref;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.users_profile);
		// ((MobileApplication) this.getApplication()).setPreference(userPref);

		listView = (ListView) findViewById(R.id.listView1);
		View header1 = (View) getLayoutInflater().inflate(
				R.layout.list_food_header, null);
		listView.addHeaderView(header1);

		View header2 = (View) getLayoutInflater().inflate(
				R.layout.rating_for_field, null);
		listView.addHeaderView(header2);

		View footer = (View) getLayoutInflater().inflate(
				R.layout.list_food_footer, null);
		listView.addFooterView(footer);

		ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter
				.createFromResource(this, R.array.foodTypeAvailable,
						android.R.layout.simple_spinner_item);
		spinner = (Spinner) footer.findViewById(R.id.foodTypeSpinner);
		spinnerAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(spinnerAdapter);

		sharedPref = getPreferences(Context.MODE_PRIVATE);
		((MobileApplication) this.getApplication()).setSharedPref(sharedPref);
		foodAdapter = new foodTypeAdapter(this, listView,
				R.layout.list_food_item, foodType, userPref, sharedPref);
		listView.setAdapter(foodAdapter);

		foodTypesPref = sharedPref.getStringSet(
				getString(R.string.food_types_preference_saving),
				new HashSet<String>());
		if (foodTypesPref != null) {
			for (String type : foodTypesPref) {
				float delta = 98;
				LayoutParams params2 = listView.getLayoutParams();
				params2.height += delta;
				int img;
				if (type.equals("Seafood"))
					img = R.drawable.seafood;
				else if (type.equals("Fastfood"))
					img = R.drawable.fastfood;
				else if (type.equals("Pizza"))
					img = R.drawable.pizza;
				else if (type.equals("Asian food"))
					img = R.drawable.asianfood;
				else if (type.equals("Vegetarian food"))
					img = R.drawable.vegetarian;
				else
					img = R.drawable.beefporkchicken;

				foodType.add(new foodType(type, img));
				foodAdapter.notifyDataSetChanged();
			}

		}
		Button addButton = (Button) footer.findViewById(R.id.Add);
		addButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				float delta = 98;
				FoodType item;
				LayoutParams params2 = listView.getLayoutParams();
				params2.height += delta;

				String type = spinner.getSelectedItem().toString();
				int img;
				if (type.equals("Seafood"))
					img = R.drawable.seafood;
				else if (type.equals("Fastfood"))
					img = R.drawable.fastfood;
				else if (type.equals("Pizza"))
					img = R.drawable.pizza;
				else if (type.equals("Asian food"))
					img = R.drawable.asianfood;
				else if (type.equals("Vegetarian food"))
					img = R.drawable.vegetarian;
				else
					img = R.drawable.beefporkchicken;
				// userPref.getFoodtypes().add(item);
				foodTypesPref.add(type);
				SharedPreferences.Editor editor = sharedPref.edit();
				editor.putStringSet(
						getString(R.string.food_types_preference_saving),
						foodTypesPref);
				editor.commit();

				foodType.add(new foodType(type, img));
				foodAdapter.notifyDataSetChanged();
			}
		});

		ratingBar1 = (RatingBar) findViewById(R.id.ratingBar1);
		ratingBar1
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						// userPref.setFoodTypeWeight(rating);
						SharedPreferences.Editor editor = sharedPref.edit();
						editor.putFloat(
								getString(R.string.food_types_weight_saving),
								rating);
						editor.commit();
						System.out.println(sharedPref
								.getFloat(
										getString(R.string.food_types_weight_saving),
										0));
					}

				});
		ratingBar2 = (RatingBar) findViewById(R.id.ratingBar2);
		ratingBar2
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						// userPref.setPriceTypeWeight(rating);
						SharedPreferences.Editor editor = sharedPref.edit();
						editor.putFloat(
								getString(R.string.price_types_weight_saving),
								rating);
						editor.commit();
						System.out.println(sharedPref.getFloat(
								getString(R.string.price_types_weight_saving),
								0));
					}

				});
		ratingBar3 = (RatingBar) findViewById(R.id.ratingBar3);
		ratingBar3
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						// userPref.setBuyToGoWeight(rating);
						SharedPreferences.Editor editor = sharedPref.edit();
						editor.putFloat(
								getString(R.string.buy_go_weight_saving),
								rating);
						editor.commit();
						System.out.println(sharedPref.getFloat(
								getString(R.string.buy_go_weight_saving), 0));
					}

				});

		// if (savedInstanceState == null) {
		// getSupportFragmentManager().beginTransaction()
		// .add(R.id.container, new PlaceholderFragment())
		// .commit();
		// }

		String priceType = sharedPref.getString(
				getString(R.string.price_type_preference_saving), "CHEAP");
		boolean buyToGo = sharedPref.getBoolean(
				getString(R.string.buy_go_preference_saving), false);
		float foodTypeWeight = sharedPref.getFloat(
				getString(R.string.food_types_weight_saving), 0);
		float priceTypeWeight = sharedPref.getFloat(
				getString(R.string.price_types_weight_saving), 0);
		float buyGoWeight = sharedPref.getFloat(
				getString(R.string.buy_go_weight_saving), 0);

		cheapButton = (RadioButton) findViewById(R.id.cheapPrice);
		mediumButton = (RadioButton) findViewById(R.id.mediumPrice);
		expensiveButton = (RadioButton) findViewById(R.id.expensivePrice);
		cheapButton.setChecked(false);
		mediumButton.setChecked(false);
		expensiveButton.setChecked(false);
		if (priceType.equals("CHEAP"))
			cheapButton.setChecked(true);
		else if (priceType.equals("MEDIUM"))
			mediumButton.setChecked(true);
		else
			expensiveButton.setChecked(true);

		buyGoCheck = (CheckBox) findViewById(R.id.buyGoCheckBox);
		buyGoCheck.setChecked(buyToGo);

		ratingBar1.setRating(foodTypeWeight);
		ratingBar2.setRating(priceTypeWeight);
		ratingBar3.setRating(buyGoWeight);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.users_profile, container,
					false);
			return rootView;
		}
	}

	public void onRadioButtonClicked(View view) {
		// Is the button now checked?
		boolean checked = ((RadioButton) view).isChecked();

		// Check which radio button was clicked
		SharedPreferences.Editor editor = sharedPref.edit();

		switch (view.getId()) {
		case R.id.cheapPrice:
			if (checked) {
				// cheapButton.setChecked(true);
				// mediumButton.setChecked(false);
				// expensiveButton.setChecked(false);
				editor.putString(
						getString(R.string.price_type_preference_saving),
						"CHEAP");
				editor.commit();
			}
			// userPref.setPriceType(PriceType.CHEAP);
			break;
		case R.id.mediumPrice:
			if (checked) {
				// cheapButton.setChecked(false);
				// mediumButton.setChecked(true);
				// expensiveButton.setChecked(false);
				System.out.println("medium Price is chose");
				editor.putString(
						getString(R.string.price_type_preference_saving),
						"MEDIUM");
				editor.commit();
			}
			// userPref.setPriceType(PriceType.MEDIUM);
			break;
		case R.id.expensivePrice:
			if (checked) {
				// cheapButton.setChecked(false);
				// mediumButton.setChecked(true);
				// expensiveButton.setChecked(true);
				editor.putString(
						getString(R.string.price_type_preference_saving),
						"EXPENSIVE");
				editor.commit();
			}
			// userPref.setPriceType(PriceType.EXPENSIVE);
			break;
		}

		System.out.println(sharedPref.getString(
				getString(R.string.price_type_preference_saving), "CHEAP"));

	}

	public void onCheckboxClicked(View view) {
		// Is the view now checked?
		boolean checked = ((CheckBox) view).isChecked();
		SharedPreferences.Editor editor = sharedPref.edit();
		editor.putBoolean(getString(R.string.buy_go_preference_saving), checked);
		editor.commit();
		// userPref.setBuyToGo(checked);
		System.out.println(sharedPref.getBoolean(
				getString(R.string.buy_go_preference_saving), false));
	}

}
